/**
 *
 * 2007-11-2 下午05:42:07
 * @author xuhuan
 */

package edu.ccut.saturn.studio.logic.model.xml.loader;

import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import edu.ccut.saturn.studio.logic.model.Connection;
import edu.ccut.saturn.studio.logic.model.Shape;

public class ConnectionLoader extends NodeLoader {

	public void loadNodeForConnection(Element root, List<Shape> shapes) {
		NodeList connectionNodes = root
				.getElementsByTagName(Shape.KEY_NODE_CONNECTION);

		for (int i = 0; i < connectionNodes.getLength(); ++i) {
			Node connectionNode = connectionNodes.item(i);
			String sourceId = super.getNodeAttributeValue(connectionNode,
					Shape.KEY_CONNECTION_ATTRIBUTE_SOURCE);
			String targetId = super.getNodeAttributeValue(connectionNode,
					Shape.KEY_CONNECTION_ATTRIBUTE_TARGET);
			String style = super.getNodeAttributeValue(connectionNode, "style");
			String name = super.getNodeAttributeValue(connectionNode, "name");

			Shape source = this.getShapeByKey(shapes, sourceId);
			Shape target = this.getShapeByKey(shapes, targetId);

			if (source != null && target != null) {
				if(style.equals("1")){
					Connection conn = new Connection(source,target,Connection.SOLID_CONNECTION);
					conn.setLineName(name);
				}
				if(style.equals("2")){
					Connection conn = new Connection(source,target,Connection.DASHED_CONNECTION);
					conn.setLineName(name);
				}
				if(style.equals("6")){
					new Connection(source,target,Connection.EXCEPTION_CONNECTION);
				}
			} else {
				// TODO:这时怎么办？
			}
		}
	}

	private Shape getShapeByKey(List<Shape> shapes, String key) {
		for (Shape shape : shapes) {
			if (shape.getId().equals(key))
				return shape;
		}

		return null;
	}

	public List<Shape> loadNodes(Element root) {
		return null;
	}
}
